<template>
  <div class="comment-post">
    <van-field class="post-field"
               v-model.trim="message"
               rows="2"
               autosize
               type="textarea"
               maxlength="50"
               placeholder="请输入留言"
               show-word-limit>

    </van-field>
    <van-button slot="button"
                size="mini"
                type="primary"
                round
                class="post-btn"
                @click="onPost"
                :disabled="!message.length">
      发布
    </van-button>
  </div>
</template>

<script>
import { addComment } from '../../../api/comment.js'
export default {
  name: 'CommentPost',
  inject: {
    articleId: {
      type: [Number, String, Object],
      default: null
    }
  },
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      message: ''
    }
  },
  methods: {
    async onPost () {
      // this.articleId = Number(this.articleId.substr(0, 6))
      console.log(this.articleId)
      try {
        const { data } = await addComment({
          target: this.target.toString(),
          content: this.message,
          art_id: this.articleId ? this.articleId.toString() : this.articleId
        })
        // console.log(data)
        this.message = ''
        console.log(data.data)
        this.$emit('post-success', data.data)
        this.$toast.success('发布成功！')
      } catch (err) {
        this.$toast.fail('发布失败！')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.comment-post {
  position: relative;
  // bottom: 0;
  // left: 0;
  // width: 100%;
}
.post-btn {
  position: absolute;
  top: 70px;
  right: 30px;
}
/deep/.van-cell__value {
  margin-right: 80px;
  background-color: #f0f0f0;
  box-sizing: border-box;
  padding: 10px;
}
</style>
